// ==========================================================================
// 云捷GO自动生成model扩展代码列表、增、删，改、查、导出，只生成一次，按需修改,再次生成不会覆盖.
// 生成日期：2021-08-05 10:09:41
// 生成路径: app/model/module/shop/shop.go
// 生成人：gfcms
// ==========================================================================
package shop

import (
	"github.com/gogf/gf/database/gdb"
	"github.com/gogf/gf/errors/gerror"
	"myblog/app/utils/page"
)

//新增页面请求参数
type AddReq struct {
	Shopname  string `p:"shopname" v:"required#店铺名称不能为空"`
	Desc      string `p:"desc" `
	Listorder int    `p:"listorder" `
	Status    int    `p:"status" v:"required#状态不能为空"`
}

//修改页面请求参数
type EditReq struct {
	Id        int64  `p:"id" v:"required#主键ID不能为空"`
	Shopname  string `p:"shopname" v:"required#店铺名称不能为空"`
	Desc      string `p:"desc" `
	Listorder int    `p:"listorder" `
	Status    int    `p:"status" v:"required#状态不能为空"`
}

//分页请求参数 
type SelectPageReq struct {
	Shopname      string `p:"shopname"`      //店铺名称
	BeginTime     string `p:"beginTime"`     //开始时间
	EndTime       string `p:"endTime"`       //结束时间
	PageNum       int    `p:"pageNum"`       //当前页码
	PageSize      int    `p:"pageSize"`      //每页数
	OrderByColumn string `p:"orderByColumn"` //排序字段
	IsAsc         string `p:"isAsc"`         //排序方式
}

//根据条件分页查询数据
func SelectListByPage(param *SelectPageReq) ([]Entity, *page.Paging, error) {
	db, err := gdb.Instance()

	if err != nil {
		return nil, nil, gerror.New("获取数据库连接失败")
	}

	model := db.Model("t_shop t")

	if param != nil {

		if param.Shopname != "" {
			model.Where("t.shopname like ?", "%"+param.Shopname+"%")
		}
		if param.BeginTime != "" {
			model.Where("date_format(t.create_time,'%y%m%d') >= date_format(?,'%y%m%d') ", param.BeginTime)
		}

		if param.EndTime != "" {
			model.Where("date_format(t.create_time,'%y%m%d') <= date_format(?,'%y%m%d') ", param.EndTime)
		}
		model.Order("t.listorder desc,t.create_time desc")
	}

	total, err := model.Count()

	if err != nil {
		return nil, nil, gerror.New("读取行数失败")
	}

	page := page.CreatePaging(param.PageNum, param.PageSize, total)

	model.Limit(page.StartNum, page.Pagesize)

	if param.OrderByColumn != "" {
		model.Order(param.OrderByColumn + " " + param.IsAsc)
	}

	var result []Entity
	model.Structs(&result)
	return result, page, nil
}

// 导出excel
func SelectListExport(param *SelectPageReq) (gdb.Result, error) {
	db, err := gdb.Instance()

	if err != nil {
		return nil, gerror.New("获取数据库连接失败")
	}

	model := db.Model("t_shop t")

	if param != nil {

		if param.Shopname != "" {
			model.Where("t.shopname like ?", "%"+param.Shopname+"%")
		}
		if param.BeginTime != "" {
			model.Where("date_format(t.create_time,'%y%m%d') >= date_format(?,'%y%m%d') ", param.BeginTime)
		}

		if param.EndTime != "" {
			model.Where("date_format(t.create_time,'%y%m%d') <= date_format(?,'%y%m%d') ", param.EndTime)
		}
	}

	//"ID","店铺名称","店铺描述","排序","状态","创建时间","修改时间","更新人","创建人",
	model.Fields(" t.id ,t.shopname ,t.desc ,t.listorder ,t.status ,t.create_time ,t.update_time ,t.update_by ,t.create_by")

	result, _ := model.All()
	return result, nil
}

//获取所有数据
func SelectListAll(param *SelectPageReq) ([]Entity, error) {
	db, err := gdb.Instance()

	if err != nil {
		return nil, gerror.New("获取数据库连接失败")
	}

	model := db.Model("t_shop t")

	if param != nil {

		if param.Shopname != "" {
			model.Where("t.shopname like ?", "%"+param.Shopname+"%")
		}
		if param.BeginTime != "" {
			model.Where("date_format(t.create_time,'%y%m%d') >= date_format(?,'%y%m%d') ", param.BeginTime)
		}

		if param.EndTime != "" {
			model.Where("date_format(t.create_time,'%y%m%d') <= date_format(?,'%y%m%d') ", param.EndTime)
		}
	}

	var result []Entity
	model.Structs(&result)
	return result, nil
}

func GetShopnameId() ([]ShopId, error) {
	db, err := gdb.Instance()
	if err != nil {
		return nil, err
	}
	model := db.Model("t_shop t")
	model.Fields("t.id,t.shopname")
	model.Where("t.status=0")
	model.Order("t.id")
	var result []ShopId
	err = model.Structs(&result)
	if err != nil {
		return nil, err
	}
	return result, nil
}
